package jinyilw.swing.rlaf.ui.icon;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.Serializable;

import javax.swing.Icon;
import javax.swing.SwingConstants;
import javax.swing.plaf.UIResource;

import jinyilw.swing.graphics.GraphicsTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;

public class RArrowIcon
		implements
			Icon,
			UIResource,
			Serializable,
			SwingConstants
{
	private static final long serialVersionUID = -7614343446018204362L;
	private static Icon eastIcon, westIcon, southIcon, northIcon;
	private int w, h;
	private int direction = EAST;
	private RenderingConfig renderingConfig = new RenderingConfig();

	public RArrowIcon(int direction)
	{
		w = 14;
		h = 14;
		this.direction = direction;
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y)
	{
		int size = Math.min(h / 2, w / 2);
		size = Math.max(size, 2) / 2 * 2;
		Graphics2D g2d = (Graphics2D) g;
		renderingConfig.setAAPainting(g2d);
		g2d.setColor(RThemeColor.darkThemeColor);
		GraphicsTools.fillTriangle(g2d, x + (w - size) / 2, y + (h - size) / 2,
				size, size, direction);
		renderingConfig.resetAAPainting(g2d);
	}

	@Override
	public int getIconWidth()
	{
		return w;
	}

	@Override
	public int getIconHeight()
	{
		return h;
	}

	public static Icon getEastIcon()
	{
		if (eastIcon == null)
			eastIcon = new RArrowIcon(EAST);
		return eastIcon;
	}

	public static Icon getWestIcon()
	{
		if (westIcon == null)
			westIcon = new RArrowIcon(WEST);
		return westIcon;
	}

	public static Icon getSouthIcon()
	{
		if (southIcon == null)
			southIcon = new RArrowIcon(SOUTH);
		return southIcon;
	}

	public static Icon getNorthIcon()
	{
		if (northIcon == null)
			northIcon = new RArrowIcon(NORTH);
		return northIcon;
	}
}
